﻿@model IEnumerable<SweetSurvey.Models.ViewModels.SurveyViewModel>
@using SweetSurvey.Extentions;
@{
    ViewBag.Title = "MySurveys";
    Layout = "~/Views/Shared/_StandardLayout.cshtml";
}

<h2>MySurveys</h2>

<script type="text/javascript">
    $(function () { $('#menucontainer input#manage').attr('checked', true); });
</script>

<div id="surveys">

@foreach (var item in Model) {
    var participators = item.Participators;
    var link = Url.ActionFullPath("FillOut", "Survey", new { id = item.Id });
    <div>
        <div>
            <span style="font-size:1.2em;font-weight:bold;">@item.Title</span><br />
            @participators @if (participators <= 1) {<text> participator</text>} else {<text> participators</text>},
            created at @item.DateCreated.ToShortDateString().
        </div>
        <div style="margin-top:5px;margin-bottom:20px;font-size:0.7em;">
            <button id="view" ss:surveyId="@item.Id" ss:url="@Url.Action("Report", "Survey", new { id = item.Id })">View Report</button>
            <button id="edit" ss:surveyId="@item.Id" ss:url="@Url.Action("Edit", "Survey", new { id = item.Id })" @if (participators > 0) { <text>disabled="disabled" title="Cannot edit this survey after it starts having participator(s)."</text> }>Edit</button>
            <button id="delete" ss:surveyId="@item.Id">Delete</button>
            <input type="text" name="link" id="link" style="width:250px;padding:5px 4px 4px 6px;" class="ui-widget-content ui-corner-all" value="@link" readonly="readonly" onclick="$(this).select()" />
            <button id="open" onclick="window.open('@link')">Open this Link</button>
        </div>
    </div>
}

</div>

<script type="text/javascript">
    $(function () {
        var btnView = $('#surveys button#view');
        var btnEdit = $('#surveys button#edit');
        var btnDelete = $('#surveys button#delete');
        var btnOpen = $('#surveys button#open');
        btnView.button({ icons: { primary: 'ui-icon-lightbulb' }, text: false });
        btnEdit.button({ icons: { primary: 'ui-icon-pencil' }, text: false });
        btnDelete.button({ icons: { primary: 'ui-icon-close' }, text: false });
        btnOpen.button({ icons: { primary: 'ui-icon-newwin' }, text: false });
        // view
        var onViewClicked = function (e) {
            window.location = e.data.url;
        };
        btnView.each(function () {
            var button = $(this);
            var url = button.attr('ss:url');
            button.on('click', { url: url }, onViewClicked);
        });
        // edit
        var onEditClicked = function (e) {
            window.location = e.data.url;
        };
        btnEdit.each(function () {
            var button = $(this);
            var url = button.attr('ss:url');
            button.on('click', { url: url }, onEditClicked);
        });
        // delete
        var onDeleteClicked = function (e) {
            $confirm('Are you sure you want to delete this survey?', function () {
                // delete this survey
                $.post('@Url.Action("DeleteSurvey", "Home")', { id: e.data.surveyId }, function (reponse) {
                    if (reponse == 'success') { e.data.row.remove(); }
                });
            });
        }
        btnDelete.each(function () {
            var button = $(this);
            var surveyId = button.attr('ss:surveyId');
            var row = button.parent().parent();
            button.on('click', { surveyId: surveyId, row: row }, onDeleteClicked);
        });
    });
</script>

